package com.itheima.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: spring_base
 * @description:
 * @author: zhanghz001
 * @create: 2020-12-03 16:22
 **/
//异常处理
// @Component
public class MyExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object o, Exception e) {
        System.out.println("异常正在处理中");
        System.out.println(o);
        System.out.println(e.toString());
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof NullPointerException) {
            modelAndView.addObject("msg", "空指针: " + e.getMessage());
        } else if (e instanceof ArithmeticException) {
            modelAndView.addObject("msg", "算术: " + e.getMessage());
        } else {
            modelAndView.addObject("msg", "其他异常: " + e.getMessage());
        }
        modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
}
